package com.hziee.graduate.ticket.dao;

import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hziee.graduate.ticket.dto.R;
import com.hziee.graduate.ticket.vo.ConditionMovieVO;
import org.apache.ibatis.annotations.Param;
import com.hziee.graduate.ticket.entity.Movie;
import org.apache.ibatis.annotations.Select;


public interface MovieDao extends BaseMapper<Movie> {

    /**
     * 批量新增数据（MyBatis原生foreach方法）
     *
     * @param entities List<Movie> 实例对象列表
     * @return 影响行数
     */
    int insertBatch(@Param("entities") List<Movie> entities);

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param entities List<Movie> 实例对象列表
     * @return 影响行数
     * @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常，请自行校验入参
     */
    int insertOrUpdateBatch(@Param("entities") List<Movie> entities);

    @Select(
            "select DISTINCT m.* from t_schedule s\n" +
            "join t_cinema c on s.cinema_id = c.cinema_id \n" +
            "join t_region r on c.region_id = r.id\n" +
            "join t_movie m on s.movie_id = m.movie_id\n" +
            "join t_region r2 on r.pid = r2.id\n" +
            "where r2.name = #{locationName} and s.del_flag = 0 and s.date = #{date} and c.del_flag = 0 "
    )
    List<Movie> getScheduledMovie(@Param("locationName") String locationName, @Param("date") String date);

    List<ConditionMovieVO> getLimitMovieByCondition(
            @Param("regionId") String regionId,
            @Param("date") String date,
            @Param("movieId") String movieId,
            @Param("typeId") String typeId,
            @Param("start") Long start,
            @Param("pageSize") Integer pageSize
    );

    Long getMovieCountByCondition(
            @Param("regionId") String regionId,
            @Param("date") String date,
            @Param("typeId") String typeId,
            @Param("movieId") String movieId
    );

    @Select("select m.*\n" +
            "from t_cinema c     \n" +
            "join t_region r on c.region_id = r.id\n" +
            "join t_cinema_movie cm on cm.cinema_id = c.cinema_id\n" +
            "join t_movie m on cm.movie_id = m.movie_id \n" +
            "join t_type t on m.movie_type_id = t.type_id \n" +
            "where c.del_flag = 0 and cm.del_flag = 0 and (r.name like #{keyword} or m.movie_name like #{keyword} or t.type like #{keyword})")
    List<Movie> getMovieByKeyword(String keyword);

    @Select("select * from t_movie m\n" +
            "where not exists(\n" +
            "select * from t_cinema_movie \n" +
            "where movie_id = m.movie_id\n" +
            "and cinema_id = #{cinemaId} and del_flag = 0\n" +
            ")")
    List<Movie> getUnJoinMovieByCinema(Long cinemaId);

    @Select("select distinct  m.movie_name as movieName from t_schedule s\n" +
            "join t_cinema c on c.cinema_id = s.cinema_id\n" +
            "join t_movie m on m.movie_id = s.movie_id\n" +
            "where c.cinema_id = #{cinemaId} and s.date >= #{start} and s.date <= #{end}")
    List<String> getScheduleMovieByCinemaIdForSeven(
            @Param("start") String start,
            @Param("end") String end,
            @Param("cinemaId") Long cinemaId);
}

